package com.bw.ppjokeplay.common.network

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class ApiService private constructor(){

    companion object{
        private var apiService : ApiService ?=null
            get() {
                if (field == null)
                    field = ApiService()
                return field
            }
        fun instance ():ApiService{
            return apiService!!
        }
    }

    //构建Retrofit对象用于发起网路请求
    fun getRetrofit(): Retrofit? {
        val okBuilder : OkHttpClient.Builder = OkHttpClient.Builder()
        val loggerInterceptor : HttpLoggingInterceptor = HttpLoggingInterceptor()
        loggerInterceptor.level = HttpLoggingInterceptor.Level.BODY
        okBuilder.addInterceptor(loggerInterceptor)
        okBuilder.readTimeout(30,TimeUnit.SECONDS)
        okBuilder.connectTimeout(30,TimeUnit.SECONDS)
        okBuilder.writeTimeout(30,TimeUnit.SECONDS)
        val build : Retrofit.Builder = Retrofit.Builder()
        build.client(okBuilder.build())
        build.baseUrl(Api.BASEURL)
        build.addConverterFactory(GsonConverterFactory.create())
        build.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        return build.build()
    }

}